﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;


namespace WLC
{
    class WLCUpdateWorkloadsStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCUpdateWorkloadsStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition wlcList = schema.AddElementProperty("WLCWorkstationList", WLCWorkstationListElementDefinition.ID);
            wlcList.Description = "The WLCWorkstationList containing all workstations of the shop floor.";
            wlcList.Required = true;

            IPropertyDefinition orderSize = schema.AddStateProperty("OrderSizeState");
            orderSize.Description = "The state variable holding the size of the order";
            orderSize.Required = true;
 
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

        public string Name
        {
            get { return "UpdateWorkloads"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }


        public Guid UniqueID
        {
            get { return new Guid("{4EF77AD8-08FF-11DF-9500-D78C56D89593}"); }
        }

        #endregion
    }


    class WLCUpdateWorkloadsStep : IStep
    {
        IElementProperty _wlcWorkstationListProp;
        IStateProperty _orderSizeStateProp;

        public WLCUpdateWorkloadsStep(IPropertyReaders bag)
        {
            _wlcWorkstationListProp = (IElementProperty)bag.GetProperty("WLCWorkstationList");
            
            _orderSizeStateProp = (IStateProperty)bag.GetProperty("OrderSizeState");
           
           
        }

        #region IStep Members

        public ExitType Execute(IStepExecutionContext context)
        {
            

            WLCWorkstationListElement wlcWorkstationList = (WLCWorkstationListElement)_wlcWorkstationListProp.GetElement(context);
            
            // This step is necessary due to lack of functionality in sprint 32. Now it is possible to directly reference an entity's element
            // without having to define a property group.
            IRepeatingPropertyReader wlcSequence = (IRepeatingPropertyReader) context.AssociatedObject.Properties.GetProperty("WLCSequence");
            IState _orderSizeState = _orderSizeStateProp.GetState(context);

            int steps = wlcSequence.GetCount(context);

            String currentWorkstationName;
            double currentStepWorkload;
            double orderSize = _orderSizeState.StateValue;
            

            for (int i = 0; i < steps; i++)
            {
                // Get the name of the current workstation from the WLCOrder Repeated Property group
                currentWorkstationName = wlcSequence.GetRow(i, context).GetProperty("WorkstationName").GetStringValue(context);
                if (!currentWorkstationName.Equals("Sink"))
                {
                    
                    // Get the workload this order produces at the current workstation and multiply it with the order's size
                    currentStepWorkload = wlcSequence.GetRow(i, context).GetProperty("ProcessingTime").GetDoubleValue(context) * orderSize;

                    // Get the current workload for this workstation from the list of workstations
                    wlcWorkstationList.AddToLoadLimitForWorkstation(currentWorkstationName, currentStepWorkload);
                }


            }


            return ExitType.FirstExit;
        }

        #endregion

    }

    }


